package com.infore.water.service;

import com.infore.water.dao.StationDao;
import com.infore.water.dao.UserDao;
import com.infore.water.util.WaterLevelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StationService {

    @Autowired
    private StationDao stationDao;

    @Autowired
    private UserDao userDao;

    public List<Map<String, Object>> queryStationTree(String userId) {
        Map<String,Object> userMap = userDao.queryUserByUserId(userId);
        List<Map<String,Object>> stationList = stationDao.queryStationByRoleId(userMap.get("roleId").toString());
        List<Map<String,Object>> list = new ArrayList<>();
        for(int i=0;i<stationList.size();i++){
            boolean flag = true;
               for(int j=0;j<list.size();j++){
                   boolean site = (stationList.get(i).get("siteId")==null&&list.get(j).get("siteId")==null);
                   if(site||stationList.get(i).get("siteId").equals(list.get(j).get("siteId"))){
                       flag = false;
                       List<Map<String,Object>>  slist=(List<Map<String,Object>>) list.get(j).get("stationList");
                       Map<String,Object> map = new HashMap<>();
                       map.put("stationId",stationList.get(i).get("stationId"));
                       map.put("stationName",stationList.get(i).get("stationName"));
                       slist.add(map);
                   }
               }
            if (flag){
                Map<String,Object> siteMap = new HashMap<>();
                siteMap.put("siteId",stationList.get(i).get("siteId"));
                siteMap.put("siteName",stationList.get(i).get("siteName"));
                List<Map<String,Object>> slist = new ArrayList<>();
                Map<String,Object> map = new HashMap<>();
                map.put("stationId",stationList.get(i).get("stationId"));
                map.put("stationName",stationList.get(i).get("stationName"));
                slist.add(map);
                siteMap.put("stationList",slist);
                list.add(siteMap);
            }

        }
        return list;
    }

    public List<Map<String, Object>> getStationRanking(String userId,String pageNum,String pageSize,String sortType) {
        Map<String,Object> userMap = userDao.queryUserByUserId(userId);
        List<Map<String,Object>> list = stationDao.getStationRanking(userMap.get("roleId").toString());
        System.out.println(list.size());
        for(int i=0;i<list.size();i++){
            List<Map<String,Object>> prolist = stationDao.getStationLevel(list.get(i).get("stationId").toString());
            System.out.println(prolist.size());
            List<Integer> levelList = new ArrayList<>();
            for(int j=0;j<prolist.size();j++){
                int level = WaterLevelUtil.getProjectLevel(prolist.get(j).get("projectName").toString(),Double.parseDouble(prolist.get(j).get("value").toString()));
                levelList.add(level);
                prolist.get(j).put("level",level);
            }
            list.get(i).put("proList",prolist);
            if(levelList.size()!=0) {
                list.get(i).put("level", Collections.max(levelList));
            }else {
                list.get(i).put("level", 1);
            }
        }
        if("0".equals(sortType)){
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return  Integer.parseInt(o2.get("level").toString())- Integer.parseInt(o1.get("level").toString());
                }
            });
        }else {//正序
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return Integer.parseInt(o1.get("level").toString()) - Integer.parseInt(o2.get("level").toString());
                }
            });
        }
        List<Map<String,Object>> dataList = new ArrayList<>();
        if(pageNum!=null) {
            for (int i = 0; i < list.size(); i++) {
                if (i >= (Integer.parseInt(pageNum) * Integer.parseInt(pageSize)) && i < ((Integer.parseInt(pageNum) + 1) * Integer.parseInt(pageSize))) {
                    dataList.add(list.get(i));
                }
            }
            return dataList;
        }
        return list;
    }
}
